<?php
class Library_Model_ChiNhanhMapper extends Library_Model_DataMapperAbstract
{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchChiNhanhById($id)
	{
		$db = $this->getDb();
		$selectChinhanh = $db->select()
											->from('tbl_chinhanh')
											->where('MaChiNhanh = ?',$id);
		$chinhanh = $db->fetchRow($selectChinhanh);
	
		return $this->_populate($chinhanh);
	
	}
	public function chinhanhList()
	{
		$db = $this->getDb();
		$selectChinhanh = $db->select()
		->from('tbl_chinhanh');
		$chinhanhs = $db->fetchAll($selectChinhanh);
	
		$chinhanhObjArray = array();
		foreach($chinhanhs as $chinhanh){
			$chinhanhObjArray[] = $this->_populate($chinhanh);
		}
		
		return $chinhanhObjArray;			
	}
	
	public function _populate($data){
		$obj = new Library_Model_ChiNhanh();
		$obj->setId($data['MaChiNhanh']);
		$obj->setTenchinhanh($data['TenChiNhanh']);
		$obj->setDiachi($data['DiaChi']);
		$obj->setDienthoai($data['DienThoai']);
		
		return $obj;		
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){	
		$db = $this->getDb();
		$data =array(
				'MaChiNhanh' => $obj->getId(),
				'TenChiNhanh' => $obj->getTenchinhanh(),
				'Diachi' 			=> $obj->getDiachi(),
				'DienThoai' 		=> $obj->getDienthoai(),			
				);
		$db->insert('tbl_chinhanh',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data =array(
				'MaChiNhanh' => $obj->getId(),
				'TenChiNhanh' => $obj->getTenChiNhanh(),
				'Diachi' => $obj->getDiachi(),
				'DienThoai' => $obj->getDienthoai(),
				);
		$where = $db->quoteInto('MaChiNhanh = ?', $obj->getId());
		$db->update('tbl_chinhanh',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaChiNhanh = ?',$id);
		$db->delete('tbl_chinhanh',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
	
	}